function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
layui.use(['form','layer'], function () {

    var $ = layui.$, form = layui.form, layer=layui.layer;

    var id = '';
    var userInfo;
    $(function () {
        LoginStatus();
        id = getUrlParam('id');
        userInfo = JSON.parse($.cookie('userInfo'));

        $('#userAvatar').attr('src', companyServices + userInfo.img);
        $('#userName').text((userInfo.name == '' || userInfo.name == null) ? "":userInfo.name);
        $('#userNameOther').text((userInfo.name == '' || userInfo.name == null) ? "":userInfo.name);
        $('#userPhone').text((userInfo.tel == '' || userInfo.tel == null) ? "":userInfo.tel);
        $('#userPosition').text(userInfo.position == '' ? '人事': userInfo.position);

        //加载公司信息
        loadCompany();


        if (id != '' && id != null && id != 'undefined') {
            loadResumeDetail(id);
        }

    })

    $('#logout').click(function () {
        layer.confirm('您确认退出登录吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            logoutCompany();
        }, function(){

        });
    })

    $('#addPosition').click(function () {
        window.location.href="job-manage.html";
    })
    //加载职位列表
    function loadResumeDetail(id) {
        layer.load(2);
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/resume/delivery/" + id, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                layer.closeAll('loading');
                checkLoginStatus(res);

                if(res.status == '00200'){
                    var user = res.data.user;
                    var careerObjective = res.data.careerObjective[0];
                    var resume = res.data.resume;
                    var experienceWork = res.data.experienceWork;
                    var experienceEducation = res.data.experienceEducation;
                    var professionalSkill = res.data.professionalSkill;
                    var experienceProject = res.data.experienceProject;

                    if (user != null) {
                        $('#resumeUserName').text(user.name);

                        if (user.gender == 2) {
                            $('#resumeUserSex').attr('src', '../images/female.png');
                            $('#resumeUserSexOther').attr('src', '../images/female1.png');
                        } else {
                            $('#resumeUserSex').attr('src', '../images/male.png');
                            $('#resumeUserSex').attr('src', '../images/male1.png');
                        }

                        var thisYear =  parseInt(new Date().getFullYear());
                        var birthYear = parseInt(user.birthday.substring(0,4));
                        var workAge = 0;
                        if(user.firstWorkTime){
                            var firstWorkTime = parseInt(user.firstWorkTime.substring(0,4));
                            workAge = thisYear-firstWorkTime;
                        }
                        var age = thisYear-birthYear;

                        $('#resumeUserAge').text(age +'岁');
                        $('#resumeWorkYear').text('工作' + workAge + "年");
                        $('#resumeWorkCity').text((careerObjective.cityname == '' || careerObjective.cityname == null) ? "":careerObjective.cityname);
                        if (experienceEducation[0] != null) {
                            $('#resumeEducation').text((experienceEducation[0].educationLevelName == '' || experienceEducation[0].educationLevelName == null) ? "":experienceEducation[0].educationLevelName);
                        } else {
                            $('#resumeEducation').text('无');
                        }



                        $('#resumeUserTel').text((user.tel == '' || user.tel == null ) ? "":user.tel);
                        $('#resumeUserEmail').text((user.email == '' || user.email == null) ? "":user.email);
                        $('#resumeUserWechat').text((user.wechat == '' || user.wechat == null ) ? "":user.wechat);
                        $('#resumeUserWebSite').text((user.webSite == '' || user.webSite == null ) ? "":user.webSite);
                        $('#resumeUserAvatar').attr('src', companyServices + user.img);


                        //求职意向
                        $('#individualName').text((careerObjective.individualName == '' || careerObjective.individualName == null) ? "":careerObjective.individualName);
                        $('#positionName').text((careerObjective.positionName == '' || careerObjective.positionName == null ) ? "":careerObjective.positionName);
                        $('#salaryLevelName').text((careerObjective.salaryLevelName == '' || careerObjective.salaryLevelName == null ) ? "":careerObjective.salaryLevelName);
                        $('#cityname').text((careerObjective.cityname == '' || careerObjective.cityname == null) ? "":careerObjective.cityname);
                        $('#jobHuntingStatusName').text((careerObjective.jobHuntingStatusName == '' || careerObjective.jobHuntingStatusName == null) ? "":careerObjective.jobHuntingStatusName);


                        //自我介绍
                        $('#description').text((resume.profile == '' || resume.profile == null ) ? "":resume.profile);

                        //工作经历
                        if (experienceWork != null && experienceWork.length > 0) {
                            var experienceWorkHtml = '';
                            $.each(experienceWork, function (index, item) {
                                experienceWorkHtml += '<li> <p class="place">';
                                experienceWorkHtml += '<span>'+((item.company == "" || item.company == null) ? "":item.company)+'</span>';
                                experienceWorkHtml += '<span class="time">'+((item.entryTime == "" || item.entryTime == null) ? "":item.entryTime)+'至'+((item.separationTime == "" || item.separationTime == null) ? "":item.separationTime)+'</span>';
                                experienceWorkHtml += '</p> <p class="place-brach">';
                                experienceWorkHtml += '<span>'+((item.department == "" || item.department == null) ? "":item.department)+'</span> <span>'+((item.position == '' || item.position == null) ? "":item.position)+'</span> </p>';
                                experienceWorkHtml += '<p class="duty">'+((item.workContext == "" || item.workContext == null) ? "":item.workContext)+' </p> </li>';
                            });
                            $('#experienceList').html(experienceWorkHtml);
                        }

                        //教育经历
                        if (experienceEducation != null && experienceEducation.length > 0) {
                            var experienceEducationHtml = '';
                            $.each(experienceEducation, function (index, item) {
                                experienceEducationHtml += '<div class="education">';
                                experienceEducationHtml += '<p class="place"><span>'+((item.school == "" || item.school == null ) ? "":item.school)+'</span><span class="time">'+((item.admissionTime == "" || item.admissionTime == null) ? "":item.admissionTime)+'至'+((item.graduationTime == '' || item.graduationTime == null)?"":item.graduationTime)+'</span></p>';
                                experienceEducationHtml += '<p class="place-brach"><span>'+((item.educationLevelName == "" || item.educationLevelName == null ) ? "":item.educationLevelName)+'</span><span>'+((item.profession == "" || item.profession == null ) ? "":item.profession)+'</span></p> </div>';
                            });
                            $('#educationList').html(experienceEducationHtml);
                        }

                        //技能
                        if (professionalSkill != null && professionalSkill.length > 0) {
                            var professionalSkillHtml = '';
                            $.each(professionalSkill, function (index, item) {
                                professionalSkillHtml += '<p>'+((item.skillName == "" || item.skillName == null)?"":item.skillName)+' —— '+((item.proficiencyName == "" || item.proficiencyName == null) ? "":item.proficiencyName)+'</p>';
                            });
                            $('#professionalSkillList').html(professionalSkillHtml);
                        }

                        //项目经验
                        if (experienceProject != null && experienceProject.length > 0) {
                            var experienceProjectHtml = '';
                            $.each(experienceProject, function (index, item) {
                                experienceProjectHtml += '<li> <p class="place-brach"> <span>'+((item.projectName == "" || item.projectName == null ) ? "":item.projectName)+'</span>';
                                experienceProjectHtml += '<span>'+((item.duties == "" || item.duties == null) ? "":item.duties)+'</span> </p> <p class="duty">'+((item.workContext == "" || item.workContext == null)? "":item.workContext)+' </p> </li>';
                            });
                            $('#experienceProjectList').html(experienceProjectHtml);
                        }



                    }


                }
            }, fail:function () {
                layer.closeAll('loading');
            }
        })
    }

    $('#reject').click(function () {
        handle(2);
    })

    $('#interview').click(function () {
        handle(1);
    })

    $('#admit').click(function () {
        handle(0);
    })
    function handle(type) {
        var url = '';
        if (type == 0) {
            url = companyServices+"/resume/delivery/admit/"+id;
        } else if (type == 1) {
            url = companyServices+"/resume/delivery/interview/"+id;
        } else if (type == 2) {
            url = companyServices+"/resume/delivery/reject/"+id;
        }

        $.ajax({
            type: 'post', // 提交方式 get/post
            url: url, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                checkLoginStatus(res);

                layerMsg(res.message);
                setTimeout(function () {
                    window.location.href="resume-manage.html";
                }, 2000)
            }
        })
    }

    //加载公司信息
    function loadCompany() {
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/company", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                checkLoginStatus(res);

                if(res.status == '00200'){
                    var companyInfo = res.data;
                    //$.cookie("companyInfo", JSON.stringify(companyInfo));
                    $('#companyLogo').attr('src', companyServices + companyInfo.logo);
                    $('#companyFullName').text((companyInfo.fullName == '' || companyInfo.fullName == null) ? "":companyInfo.fullName);
                    $('#companySimpleName').text((companyInfo.simpleName == '' || companyInfo.simpleName == null) ? "":companyInfo.simpleName);
                    $('#industry').text((companyInfo.industryName == '' || companyInfo.industryName == null) ? "":companyInfo.industryName);
                    $('#companyLevel').text((companyInfo.companySizeLevelName == '' || companyInfo.companySizeLevelName == null ) ? "":companyInfo.companySizeLevelName);


                    if (companyInfo.rootId === userInfo.id) {
                        $('#hrManager').show();
                    }
                }
            }
        })
    }


});




